from typing import Union, List
from pydantic import BaseModel, Field
from tortoise.contrib.pydantic import pydantic_queryset_creator, pydantic_model_creator

from models.chat import ChatMessage, ChatApplication, ChatSession


class ChatMessageListPydantic(BaseModel):
    content: str = Field(min_length=1)
    role: str = Field(min_length=1, max_length=200)


class ChatMessagePydantic(BaseModel):
    message: str = Field(max_length=200, default="")
    message_list: List[ChatMessageListPydantic] = []
    chat_id: Union[str, None] = None


class ChatApplicationPydantic(BaseModel):
    title: str = Field(min_length=1, max_length=255)
    desc: str = Field(min_length=1)
    icon: str = Field(min_length=1)
    instruct: str = Field(min_length=1, description="指令")
    examples: str = Field(min_length=1, max_length=200, description="示例模板")
    user_id: int = None
    application_type: int = 1


class ChatApplicationMessagePydantic(BaseModel):
    application_id: int = None
    instruct: str = Field(min_length=1, description="指令", default="")
    examples: str = Field(min_length=1, max_length=200, description="示例模板")


class ChatApplicationSearchPydantic(BaseModel):
    search_value: str = None


class ChatSessionMessagePydantic(BaseModel):
    session_id: int


ChatMessagePydanticList = pydantic_queryset_creator(ChatMessage, exclude=("owner_user", "owner_chat", ))
ChatApplicationPydanticList = pydantic_queryset_creator(ChatApplication,
                                                        exclude=("user_id", "application_type", "instruct",))
ChatSessionPydanticList = pydantic_queryset_creator(ChatSession, exclude=("owner_key", "user_id",))
ChatApplicationPydanticModel = pydantic_model_creator(ChatApplication)
